package com.sample.client.ui

import android.content.Intent
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import androidx.appcompat.app.AppCompatActivity
import com.loyal.client.ClientConfig
import com.loyal.client.ClientHelper
import com.loyal.client.error.ExceptHelper
import com.loyal.client.impl.DialogCallback
import com.loyal.client.impl.SubscriberCallback
import com.sample.client.HttpSubscriber
import com.sample.client.databinding.ActivityMainBinding
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity(), SubscriberCallback<String?>, DialogCallback {
    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.textLog.movementMethod = ScrollingMovementMethod.getInstance()
        initViews()
    }

    private fun initViews() {
        binding.testGet.setOnClickListener {
            binding.textLog.text = ""
            request(2, "GET", false) }
        binding.testPost.setOnClickListener {
            binding.textLog.text = ""
            request(4, "POST", true) }
        binding.jtwf.setOnClickListener {
            binding.textLog.text = ""
            val config = ClientConfig.getInstance()
            config.url = "192.168.0.155"
            config.port = "9080"
            config.nameSpace = "jtwf_self"
            request(config, "155JTWF")
        }

        binding.mwm.setOnClickListener {
            binding.textLog.text = ""
            val config = ClientConfig.getInstance()
            config.url = "192.168.0.110"
            config.port = "8080"
            config.nameSpace = "mwm"
            request(config, "110MWM")
        }

        binding.out10.setOnClickListener {
            binding.textLog.text = ""
            val builder = OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
            val config = ClientConfig.getInstance()
            config.okCtBuilder = builder
            config.url = "192.168.0.155"
            config.port = "9080"
            config.nameSpace = "jtwf_self"
            request(config, "out10")
        }

        binding.out8.setOnClickListener {
            binding.textLog.text = ""
            val builder = OkHttpClient.Builder()
                .connectTimeout(8, TimeUnit.SECONDS)
                .writeTimeout(8, TimeUnit.SECONDS)
                .readTimeout(8, TimeUnit.SECONDS)
            val config = ClientConfig.getInstance()
            config.okCtBuilder = builder
            config.url = "192.168.0.110"
            config.port = "8080"
            config.nameSpace = "mwm"
            request(config, "out8")
        }
        binding.kt.setOnClickListener {
            val intent = Intent(this, KotlinActivity::class.java)
            startActivity(intent)
        }
    }

    private fun request(config: ClientConfig, tag: String?) {
        val subscriber = HttpSubscriber<String>(config)
        subscriber.withContext(this).setWhat(88).setTag(tag).setSubscribeListener(this)
        subscriber.showProgressDialog(true)
        ClientHelper.callExecute(subscriber.test("00001", "qazwsx"), subscriber)
    }

    private fun request(what: Int, tag: Any?, show: Boolean) {
        val jokUrl = "https://api.apiopen.top/api/getImages?type=phone&page=0&size=10"
        val subscriber = HttpSubscriber<String>()
        subscriber.setWhat(what).setTag(tag).setSubscribeListener(this)
        subscriber.showProgressDialog(show)
        if (!show) subscriber.setDialogCallback(this)
        subscriber.showDialog()
        ClientHelper.callExecute(subscriber.request(jokUrl), subscriber)
    }

    override fun onResult(what: Int, tag: Any?, result: String?) {
        binding.textLog.append("<Result>$tag : $result")
    }

    override fun onError(what: Int, tag: Any?, e: Throwable) {
        binding.textLog.append("<Error>$tag : ${ExceptHelper.get(e)}")
    }

    override fun onDialogCallback(what: Int) {
        when (what) {
            DialogCallback.SHOW -> {
                binding.textLog.append("显示加载框...")
            }

            DialogCallback.DISMISS -> {
                binding.textLog.append("隐藏加载框...")
            }
        }
    }

    companion object {
        private const val TAG = "MainActivity"
    }
}